#__Author__  = white

class basemodule(object):
    def __init__(self,argvs,db_models):
        self.db_models = db_models
        self.argvs = argvs
    def fetch_host(self):
        #提取出来所有的主机名办法
        print('---fetch host---\n----提取出来所有的主机名办法---')
        if '-h' in self.argvs or '-g' in self.argvs:
            host_list = []
            if '-h'  in self.argvs:
                host_index = self.argvs.index('-h')+1
                if len(self.argvs)>host_index:
                    host_str = self.argvs[host_index]
                    host_str_list = host_str.split(',')
                    host_list += self.db_models.Host.objects.filter(hostname__in=host_str_list)

            if '-g' in self.argvs:
                group_index = self.argvs.index('-g')+1
                if len(self.argvs)>group_index:
                    group_str = self.argvs[group_index]
                    group_str_list = group_str.split(',')
                    group_list = self.db_models.HostGroup.objects.filter(name__in=group_str_list)

                    for group in group_list:
                        group_hosts = group.hosts.select_related()
                        host_list += group_hosts

            hosts = set(host_list)

            print(hosts)
            return True

        else:
            print('\033[1;31mERROR: host[-h] or group [-g] argument must be provided\033[0m')



